
package combat.dudes.client.gui.threads.map.main_old;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.gui.ClientGUI;
import combat.dudes.client.gui.images.Painter;
import combat.dudes.client.gui.threads.map.ClientGUIMap.MapType;
import combat.dudes.client.gui.threads.map.GUIMapVehicle;
import combat.dudes.client.gui.threads.shell.GUIShell;
import combat.dudes.protocol.data.DoublePoint;
import combat.dudes.protocol.interfaces.VehicleInt;


/* FILE IS OUT OF USE!!! */
/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIMainMap {

	private final static JPanel myPanel = GUIShell.windowWorldView.myBodyPanel;
	private final static int mapWidth = myPanel.getWidth();
	private final static double mapWidthHalf = mapWidth / 2;
	private final static int mapHeight = myPanel.getHeight();
	private final static double mapHeightHalf = mapHeight / 2;

	private static Image land = DudesFunctions.File.getImage("land.gif");

	private static BufferedImage radarlinesImg = null;

	private static DoublePoint viewCenter = new DoublePoint();
	public static synchronized DoublePoint getViewCenter() {
		return viewCenter;
	}
	public static synchronized void setViewCenter(DoublePoint center) {
		viewCenter = center;
	}
	public static synchronized void resetViewCenter() {
		viewCenter = new DoublePoint(0, 0);
	}

	private static class PointToMapConverter {
		public static synchronized DoublePoint convert(DoublePoint point) {
			return point.add(mapWidthHalf, mapHeightHalf).add(viewCenter);
		}
		public static synchronized DoublePoint convert(double x, double y) {
			return convert(new DoublePoint(x, y));
		}
		public static synchronized double getOx() {
			return convert(0, 0).getX();
		}
		public static synchronized double getOy() {
			return convert(0, 0).getY();
		}
	}


	private Point source = null;
	private Boolean undoReset = false;
	private DoublePoint oldCenter = new DoublePoint();
	private int oldZoom = 1;
	public GUIMainMap() {
		// Map (background)
		JPanel map = new JPanel() {
			private static final long serialVersionUID = 1L;

			@Override
			public void paintComponent(Graphics g) {
				/* Graphics2D g2 = (Graphics2D) g; // Make type Graphics2D
				 * g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on
				 * g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // Set scaling type
				 * g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed
				 * 
				 * int landW = land.getWidth(this), landWhalf = 1534;
				 * int landH = land.getHeight(this), landHhalf = 1534;
				 * int zoom = 250 * (ClientGUI.getZoom() - 1);
				 * 
				 * g2.drawImage(land, (int) getViewCenter().getX() - (landWhalf + zoom / 2), (int) getViewCenter().getY() - (landHhalf + zoom / 2), (int) (landW + zoom), (int)
				 * (landH + zoom), this);
				 * g2.drawImage(land, (int) (getViewCenter().getX() - landWhalf + mapWidthHalf), (int) (getViewCenter().getY() - landHhalf + mapHeightHalf), (landW + zoom), (landH
				 * + zoom),
				 * this); */
				g.drawRect(0, 0, this.getWidth(), this.getHeight());
			}
		};


		// JPanel map = new GUIMapLand();


		// Cursor DUDES_CURSOR = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage(""), new Point(0, 0), "cursor");
		// test.setCursor(DUDES_CURSOR);
		map.addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseDragged(MouseEvent m) {
				// System.out.println("d(" + m.getX() + "," + m.getY() + ")");
				if (SwingUtilities.isRightMouseButton(m)) {
					GUIMainMap.myPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
					undoReset = false;

					// GUIMainMap.myPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
					setViewCenter(getViewCenter().add(-1 * (m.getX() - source.getX()), -1 * (m.getY() - source.getY())));
				}
			}
			@Override
			public void mouseMoved(MouseEvent m) {
				// System.out.println("m(" + m.getX() + "," + m.getY() + ")");
			}
		});
		map.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent m) {
				if (SwingUtilities.isMiddleMouseButton(m)) {
					if (undoReset) {
						setViewCenter(oldCenter);
						ClientGUI.setZoom(oldZoom);
						undoReset = false;
					} else {
						oldCenter = getViewCenter();
						oldZoom = ClientGUI.getZoom();
						resetViewCenter();
						ClientGUI.setZoom(1);
						undoReset = true;
					}
				}
			}
			@Override
			public void mouseEntered(MouseEvent m) {}
			@Override
			public void mouseExited(MouseEvent m) {}
			@Override
			public void mousePressed(MouseEvent m) {
				source = m.getPoint();
			}
			@Override
			public void mouseReleased(MouseEvent m) {
				GUIMainMap.myPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
				// setViewCenter(getViewCenter().add((m.getX() - source.getX()), (m.getY() - source.getY())));
			}
		});
		map.addMouseWheelListener(new MouseWheelListener() {
			@Override
			public void mouseWheelMoved(MouseWheelEvent m) {
				ClientGUI.setZoom(ClientGUI.getZoom() + (-1 * m.getWheelRotation()));
				radarlinesImg = null;
			}
		});

		map.setBounds(0, 0, mapWidth, mapHeight);


		// Radar outer lines (overlay and static image)
		final int radarlinesPad = 6;
		JPanel radarlines = new JPanel() {
			private static final long serialVersionUID = 1L;
			@Override
			public void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D) g; // Make type Graphics2D

				if (radarlinesImg == null) {
					// http://www.java.net/node/683925
					radarlinesImg = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_ARGB);
					Graphics2D radarlinesImgGfx = (Graphics2D) radarlinesImg.getGraphics();
					radarlinesImgGfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on for shapes
					radarlinesImgGfx.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // Set AA off for text
					radarlinesImgGfx.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed
					radarlinesImgGfx.setBackground(new Color(0, 0, 0, 0));
					radarlinesImgGfx.clearRect(0, 0, mapWidth, mapHeight);

					// Add compass lines on the outside of the map
					radarlinesImgGfx.create(0, 0, mapWidth, mapHeight);
					radarlinesImgGfx.setColor(new Color(255, 255, 255, 120)); // White, 100/255 transparency
					radarlinesImgGfx.drawLine(10, 10, 20, 20);
					for (int a = 0; a < 360; a++) {
						if (a != 0 && a != 90 && a != 180 && a != 270) {
							double angle = Math.toRadians(a);
							radarlinesImgGfx.drawLine(
									(int) Math.floor(mapWidthHalf),
									(int) Math.floor(mapHeightHalf),
									(int) Math.floor(mapWidthHalf + mapWidth * Math.cos(angle)),
									(int) Math.floor(mapHeightHalf + mapHeight * Math.sin(angle)));
						}
					}
					radarlinesImgGfx.clearRect(radarlinesPad, radarlinesPad, mapWidth - (radarlinesPad * 2), mapHeight - (radarlinesPad * 2));

					// Add N,W,S,E
					int fontSize = 12;
					int dirPosX[] = {
							radarlinesPad + 4,
							(int) mapWidthHalf + 4,
							mapWidth - 7 - radarlinesImgGfx.getFontMetrics().charWidth('N') };
					int dirPosY[] = {
							radarlinesPad + fontSize,
							(int) mapHeightHalf - 3,
							mapHeight - (radarlinesPad + 4) };

					radarlinesImgGfx.setFont(new Font("Lucida Console", Font.PLAIN, fontSize));

					radarlinesImgGfx.drawString("NW", dirPosX[0], dirPosY[0]);
					radarlinesImgGfx.drawString("N", dirPosX[1], dirPosY[0]);
					radarlinesImgGfx.drawString("NE", dirPosX[2] - radarlinesImgGfx.getFontMetrics().charWidth('E'), dirPosY[0]);

					radarlinesImgGfx.drawString("W", dirPosX[0], dirPosY[1]);
					radarlinesImgGfx.drawString("E", dirPosX[2], dirPosY[1]);

					radarlinesImgGfx.drawString("SW", dirPosX[0], dirPosY[2]);
					radarlinesImgGfx.drawString("S", dirPosX[1], dirPosY[2]);
					radarlinesImgGfx.drawString("SE", dirPosX[2] - radarlinesImgGfx.getFontMetrics().charWidth('E'), dirPosY[2]);
				}

				g2.drawImage(radarlinesImg, null, 0, 0);

				// Draw crosshairs
				g2.setColor(new Color(255, 255, 255, 75)); // White, 75/255 transparency
				g2.drawLine((int) PointToMapConverter.getOx(), 0, (int) PointToMapConverter.getOx(), mapHeight);
				g2.drawLine(0, (int) PointToMapConverter.getOy(), mapWidth, (int) PointToMapConverter.getOy());
			}
		};
		radarlines.setBounds(0, 0, mapWidth, mapHeight);
		myPanel.add(radarlines, JLayeredPane.DEFAULT_LAYER);

		// Radar circles (overlay)
		JPanel circles = new JPanel() {
			private static final long serialVersionUID = 1L;
			@Override
			public void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D) g; // Make type Graphics2D
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on
				g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); // Set render focus on quality

				g2.setColor(new Color(255, 255, 255, 70)); // White, 100/255 transparency

				// Add circles as an overlay (for easier (visual) measurements)
				for (int i = (250 * ClientGUI.getZoom()); i < (mapWidth * 2); i += 250 * ClientGUI.getZoom())
					g2.drawOval((int) mapWidthHalf - (i / 2), (int) mapHeightHalf - (i / 2), i, i);
			}
		};
		myPanel.add(circles, JLayeredPane.MODAL_LAYER);


		// Objects (movement!)
		JPanel boats = new JPanel() {
			private static final long serialVersionUID = 1L;
			@Override
			public void paintComponent(Graphics g) {
				Graphics2D g2 = (Graphics2D) g; // Make type Graphics2D
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on
				g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); // Set render focus on speed

				// Boats
				// TODO Optimization by caching already drawn vehicles
				GUIMapVehicle vehicleImg = null;
				ArrayList<VehicleInt> vehiclesI = ClientApp.SharedVars.getVehicles();
				if (vehiclesI != null) {
					synchronized (vehiclesI) {
						for (VehicleInt vehicle : vehiclesI) {
							DoublePoint pos = PointToMapConverter.convert(vehicle.getPosition().getX() * ClientGUI.getZoom() - GUIMapVehicle.g2Whalf,
									vehicle.getPosition().getY() * ClientGUI.getZoom() - GUIMapVehicle.g2Hhalf);
							vehicleImg = new GUIMapVehicle(MapType.MAIN, vehicle, false);
							g2.drawImage(vehicleImg.getImage(), null, (int) pos.getX(), (int) pos.getY());
						}
					}
				}
			}
		};
		myPanel.add(boats, JLayeredPane.PALETTE_LAYER);


		// Radar icon (boat)
		// GUIMainMapVehicle radarboat = new GUIMainMapVehicle();
		// radarboat.setBounds((int) posBoat.getX(), (int) (300 - ((-1) * posBoat.getX())), radarboat.getWidth(), radarboat.getHeight());
		// myPanel.add(radarboat, JLayeredPane.PALETTE_LAYER);


		myPanel.add(map, JLayeredPane.DEFAULT_LAYER); // Background (map) must be added last!


		int i = 0;
		while (true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				ClientApp.onError("", e, true);
			}
			System.out.println(i);
			posBoat.setXY(i % 200, i % 200);
			if (ClientApp.SharedVars.getVehicles().size() == 2) {
				ClientApp.SharedVars.getVehicles().get(0).setPosition(new DoublePoint(posBoat.getX(), posBoat.getY()));
				ClientApp.SharedVars.getVehicles().get(1).setPosition(new DoublePoint(-posBoat.getX(), posBoat.getY()));
			}
			// System.out.println("oihoihoih" + ClientApp.SharedVars.getVehicles().size());
			// boats.setBounds((int) posBoat.getX(), (int) posBoat.getY(), 20, 20);

			/* for(Component panel : myPanel.getComponents())
			 * panel.setBounds(0, 0, mapWidth + (i % 3), mapHeight + (i % 3)); */

			// radarlines.setBounds(0, 0, mapWidth + (i % 2), mapHeight + (i % 2));
			// circles.setBounds(0, 0, mapWidth + (i % 2), mapHeight + (i % 2));
			// boats.setBounds(0, 0, mapWidth + (i % 2), mapHeight + (i % 2));

			// Painter.repaint(boats);
			Painter.repaint(myPanel);

			i++;
		}

	}
	private DoublePoint posBoat = new DoublePoint();
}
